<template>
  <div class="today_work">
    <div style="width: 100%; padding: 10px">
      <div class="back" @click="backHome()">
        <el-icon style="font-size: 22px; color: var(--el-color-warning)">
          <HomeFilled />
        </el-icon>
      </div>
      <div
        style="
          margin-bottom: 20px;
          color: white;
          text-align: center;
          /*color: #f8d57e;*/
          font-size: 28px;
          font-family: Montserrat;
          font-weight: 700;
          text-transform: uppercase;
          letter-spacing: 0.2px;
          word-wrap: break-word;
          text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2); /* 水平偏移量 垂直偏移量 模糊半径 阴影颜色 */
        "
      >
        TRANSACTION_ASSISTANT
      </div>
    </div>
    <div class="main">
      <el-card class="left-card">
        <div
          style="
            color: var(--el-color-warning);
            font-size: 22px;
            padding-left: 10px;
            padding-top: 10px;
            padding-bottom: 20px;
            border-bottom: 1px var(--el-color-warning) solid;
          "
        >
          <div>
            <el-icon><Management /></el-icon>
            全部事务
          </div>
        </div>

        <el-scrollbar height="700">
          <div v-for="task in taskList" :key="task.id">
            <div class="task-item">
              <div style="display: inline-block; height: 100%; width: 100px">
                <div
                  style="
                    font-size: 17px;
                    color: var(--el-color-warning);
                    margin-left: 15%;
                    margin-top: 10px;
                  "
                >
                  {{ task.taskTitle }}
                </div>
              </div>
              <div
                style="
                  display: inline-block;
                  height: 100%;
                  width: 180px;
                  font-size: 16px;
                  color: var(--el-color-warning);
                  vertical-align: top;
                "
              >
                <div style="display: inline-block">
                  <div style="margin-top: 15px; margin-bottom: 10px">
                    <el-icon><AlarmClock /></el-icon>
                    {{ task.time.slice(11, 16) }}
                  </div>
                  <div>{{ task.time.slice(0, 10) }}</div>
                </div>
                <div
                  style="
                    display: inline-block;
                    margin-left: 20px;
                    vertical-align: top;
                  "
                >
                  <div
                    style="margin-top: 15px; margin-bottom: 10px"
                    @click="swapStatus(task.id, task.completionStatus)"
                  >
                    <span v-if="task.completionStatus == '1'">
                      <el-icon><Checked /></el-icon>
                      已完成
                    </span>
                    <span v-else>
                      <el-icon><Failed /></el-icon>
                      未完成
                    </span>
                  </div>
                </div>
              </div>
              <div
                style="
                  float: right;
                  height: 100%;
                  width: 100px;
                  color: var(--el-color-warning);
                  font-size: 18px;
                  padding-top: 15px;
                "
              >
                <div
                  style="
                    margin-left: 10px;
                    margin-bottom: 10px;
                    display: flex;
                    align-items: center;
                  "
                >
                  <el-icon><CollectionTag /></el-icon>
                  <span style="font-size: 14px">
                    {{
                      ReminderLevel[
                        Object.keys(ReminderLevel)[task.reminderLevel - 1]
                      ]
                    }}
                  </span>
                </div>
                <el-popconfirm
                  width="220"
                  confirm-button-text="OK"
                  :icon="InfoFilled"
                  icon-color="#626AEF"
                  :title="task.content"
                >
                  <template #reference>
                    <el-icon style="margin-right: 10px"><ZoomIn /></el-icon>
                  </template>
                </el-popconfirm>

                <el-icon
                  style="margin-right: 10px"
                  @click="
                    (changeTaskDialogVisible = true),
                      (changeTaskForm.id = task.id)
                  "
                >
                  <Edit />
                </el-icon>
                <el-icon
                  @click="
                    (deleteTaskDialogVisible = true), (deleteTaskId = task.id)
                  "
                >
                  <Delete />
                </el-icon>
              </div>
            </div>
          </div>
        </el-scrollbar>
      </el-card>

      <div class="mid-card">
        <div
          style="
            background-color: white;
            padding: 15px;
            margin-bottom: 30px;
            font-size: 26px;
            border-radius: 50px;
            box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2); /* 设置盒子阴影效果 */
          "
          @click="intoAddTransaction"
        >
          <el-icon><CirclePlusFilled /></el-icon>
          添加事务
        </div>
        <div
          style="
            background-color: white;
            padding: 15px;
            margin-bottom: 30px;
            font-size: 26px;
            border-radius: 50px;
            box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2); /* 设置盒子阴影效果 */
          "
          @click="intoAddWeeklyPlan"
        >
          <el-icon style="margin-right: 10px"><EditPen /></el-icon>
          添加周计划
        </div>
      </div>

      <el-card class="right-card">
        <div
          style="
            color: var(--el-color-warning);
            font-size: 22px;
            padding-left: 10px;
            padding-top: 10px;
            padding-bottom: 20px;
            border-bottom: 1px var(--el-color-warning) solid;
          "
        >
          <div>
            <el-icon><List /></el-icon>
            周计划
          </div>
        </div>

        <el-scrollbar height="700" v-loading="loading">
          <el-collapse accordion>
            <el-collapse-item v-for="plan in weeklyPlans" :key="plan.id">
              <template #title>
                <div
                  style="display: flex; align-items: center; color: var(--el-color-warning)"
                  @click="getTasksFromPlan(plan.id)"
                >
                  <span
                    style="
                      font-size: 17px;
                      color: var(--el-color-warning);
                      margin-left: 20px;
                      margin-right: 30vh;
                    "
                  >
                    <el-icon style="margin-right: 5px"><Document /></el-icon>
                    {{ plan.planName }}
                  </span>
                  <el-icon
                    style="margin-right: 5px; font-size: 18px"
                    @click="deletePlan(plan.id)"
                  >
                    <Delete />
                  </el-icon>
                </div>
              </template>
              <div v-for="taskFromPlan in tasksFromPlan" :key="taskFromPlan.id">
                <div class="task-item">
                  <div
                    style="display: inline-block; height: 100%; width: 100px"
                  >
                    <div
                      style="
                        font-size: 17px;
                        color: var(--el-color-warning);
                        margin-left: 15%;
                        margin-top: 10px;
                      "
                    >
                      {{ taskFromPlan.taskTitle }}
                    </div>
                  </div>
                  <div
                    style="
                      display: inline-block;
                      height: 100%;
                      width: 180px;
                      font-size: 16px;
                      color: var(--el-color-warning);
                      vertical-align: top;
                      padding-left: 20px;
                    "
                  >
                    <div style="display: inline-block">
                      <div style="margin-top: 15px; margin-bottom: 10px">
                        <el-icon><AlarmClock /></el-icon>
                        {{ taskFromPlan.time.slice(11, 16) }}
                      </div>
                    </div>
                    <div
                      style="
                        display: inline-block;
                        margin-left: 20px;
                        vertical-align: top;
                      "
                    >
                      <div style="margin-top: 15px; margin-bottom: 10px">
                        {{ taskFromPlan.dayOfWeek }}
                      </div>
                    </div>
                  </div>
                  <div
                    style="
                      float: right;
                      height: 100%;
                      width: 100px;
                      color: var(--el-color-warning);
                      font-size: 18px;
                      padding-top: 15px;
                    "
                  >
                    <div
                      style="
                        margin-left: 10px;
                        margin-bottom: 0px;
                        display: flex;
                        align-items: center;
                      "
                    >
                      <el-icon><CollectionTag /></el-icon>
                      <span style="font-size: 14px">
                        {{
                          ReminderLevel[
                            Object.keys(ReminderLevel)[
                              taskFromPlan.reminderLevel - 1
                            ]
                          ]
                        }}
                      </span>
                    </div>
                    <el-popconfirm
                      width="220"
                      confirm-button-text="OK"
                      :icon="InfoFilled"
                      icon-color="#626AEF"
                      :title="taskFromPlan.content"
                    >
                      <template #reference>
                        <el-icon style="margin-right: 10px"><ZoomIn /></el-icon>
                      </template>
                    </el-popconfirm>
                    <el-icon
                      style="margin-right: 10px"
                      @click="
                        (changeTaskDialogVisible = true),
                          (changeTaskForm.id = task.id)
                      "
                    >
                      <Edit />
                    </el-icon>
                    <el-icon
                      @click="
                        (deleteTaskDialogVisible = true),
                          (deleteTaskId = taskFromPlan.id)
                      "
                    >
                      <Delete />
                    </el-icon>
                  </div>
                </div>
              </div>
            </el-collapse-item>
          </el-collapse>
        </el-scrollbar>
      </el-card>
    </div>

    <el-dialog
      v-model="deleteTaskDialogVisible"
      title="事务删除"
      width="500"
      align-center
      style="border-radius: 30px"
    >
      <span>确认删除该事务吗？</span>
      <template #footer>
        <div class="dialog-footer">
          <el-button link @click="deleteTaskDialogVisible = false">
            取消
          </el-button>
          <el-button link type="warning" @click="taskDelete">确认</el-button>
        </div>
      </template>
    </el-dialog>

    <el-dialog
      style="border-radius: 30px; min-height: 40%; padding: 20px"
      v-model="changeTaskDialogVisible"
      width="40%"
      draggable
    >
      <div style="font-size: 23px; color: var(--el-color-warning); margin-left: 20px">
        修改事务
      </div>
      <div style="display: inline-block; width: 80%; height: 80%">
        <div class="item">
          <div class="label">提醒时间</div>
          <el-date-picker
            v-model="changeTaskForm.time"
            type="datetime"
            placeholder="Select date and time"
            :default-time="defaultTime"
          />
        </div>
        <div class="item">
          <div class="label">提醒级别</div>
          <div
            class="input-box"
            style="background-color: white; height: 70px; width: 240px"
          >
            <el-radio-group
              v-model="changeTaskForm.reminderLevel"
              style="margin: 0 auto; margin-top: 20px"
            >
              <el-radio label="1" size="small">一般</el-radio>
              <el-radio label="2" size="small">重要</el-radio>
              <el-radio label="3" size="small">关键</el-radio>
            </el-radio-group>
          </div>
        </div>
      </div>

      <div style="text-align: center; margin-top: 10%">
        <button
          style="
            height: 50px;
            width: 120px;
            border: 0;
            background-color: var(--el-color-warning);
            color: white;
            border-radius: 50px;
            font-size: 16px;
            font-weight: lighter;
            box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2); /* 设置盒子阴影效果 */
          "
          @click="changeTask"
        >
          确认修改
        </button>
      </div>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { InfoFilled } from '@element-plus/icons-vue'
import {
  getTodayTask,
  getAllTask,
  addTask,
  editTodayTask,
  editReminderLevel,
  deleteTask,
  changeStatus,
} from '@/api/task/index'
import {
  getWeeklyPlans,
  getTaskFromWeeklyPlan,
  deleteWeeklyPlanById,
} from '@/api/weeklyplan'
import { AllTaskList, EditTaskParams } from '@/api/task/type'
import { WeeklyPlan } from '@/api/weeklyplan/type'
import { ref, onMounted, reactive } from 'vue'

const router = useRouter()
const completionStatus = ref<number>(1)
const deleteTaskDialogVisible = ref<boolean>(false)
let deleteTaskId = ref<number>(0)
const changeTaskDialogVisible = ref<boolean>(false)
const changeTaskForm = reactive<EditTaskParams>({
  id: 0,
  time: '',
  reminderLevel: 0,
})
const ReminderLevel = {
  common: '一般',
  important: '重要',
  vital: '关键',
}
const loading = ref<boolean>(false)
const backHome = () => {
  router.push('/home')
}
const intoAddTransaction = () => {
  router.push('/add-transaction')
}
const intoAddWeeklyPlan = () => {
  router.push('/add-week-plan')
}
onMounted(async () => {
  await getListTask()
  await getAllPlans()
})
/**
 * 获取全部事务信息
 */
const taskList = ref<AllTaskList[]>()

const getListTask = async () => {
  try {
    const { data } = await getAllTask()
    taskList.value = data
    console.log(taskList.value)
  } catch (e) {
    Promise.reject(e)
  }
}
const taskDelete = async () => {
  try {
    const { message } = await deleteTask(deleteTaskId.value)
    ElMessage({
      showClose: true,
      message: message,
      type: 'success',
    })
  } catch (e) {
    Promise.reject(e)
  }
  await getListTask()
  await getTasksFromPlan(checkPlanId.value)
  deleteTaskDialogVisible.value = false
}
const changeTask = async () => {
  try {
    const { message } = await editTodayTask(changeTaskForm)
    ElMessage({
      showClose: true,
      message: message,
      type: 'success',
    })
  } catch (e) {
    Promise.reject(e)
  }
  await getListTask()
  changeTaskDialogVisible.value = false
}
const swapStatus = async (id: number, status: number) => {
  try {
    const { message } = await changeStatus({
      taskId: id,
      completionStatus: -(status - 1),
    })
    ElMessage({
      showClose: true,
      message: message,
      type: 'success',
    })
  } catch (e) {
    Promise.reject(e)
  }
  await getListTask()
}

/**
 * 周计划处理逻辑
 */
const weeklyPlans = ref<WeeklyPlan[]>()
const getAllPlans = async () => {
  try {
    const { data } = await getWeeklyPlans()
    weeklyPlans.value = data
  } catch (e) {
    Promise.reject(e)
  }
}
const tasksFromPlan = ref<AllTaskList[]>()
const checkPlanId = ref<number>(0)
const getTasksFromPlan = async (id: number) => {
  loading.value = true
  try {
    const { data } = await getTaskFromWeeklyPlan(id)
    checkPlanId.value = id
    tasksFromPlan.value = data
    console.log(tasksFromPlan.value)
  } catch (e) {
    Promise.reject(e)
  }
  loading.value = false
}
const deletePlan = async (id: number) => {
  ElMessageBox.confirm('确认删除该周计划吗?', '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(async () => {
      try {
        const { message } = await deleteWeeklyPlanById(id)
        await getAllPlans()
        ElMessage({
          showClose: true,
          message: message,
          type: 'success',
        })
      } catch (e) {
        Promise.reject(e)
      }
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消删除周计划',
      })
    })
}
</script>

<style scoped>
.today_work {
  width: 100%;
  height: 100%;
  /*display: flex;*/
  /*background: #b6a88e;*/
  position: relative;
  background-color: #eebe77;
  /*opacity: 0.85;*/
}
.back {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 40px;
  height: 40px;
  background-color: white;
  box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2); /* 设置盒子阴影效果 */
  border-radius: 8px;
  margin-top: 30px;
  margin-left: 20px;
}
.main {
  display: flex;
  height: 81vh;
  align-items: center; /* 在交叉轴上居中对齐元素 */
  justify-content: space-between; /* 在主轴上均匀分布元素，让左右两个卡片靠边排列 */
  background-image: url('./src/assets/images/background-coffee.jpg'); /* 替换为你的图片路径 */
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}
.left-card {
  display: inline-block;
  width: 30%;
  height: 80vh;
  margin-left: 10%;
  border-radius: 36px; /* 设置圆角的半径，可以根据需要调整 */
}
.task-item {
  margin-top: 10px;
  margin-left: 2%;
  /*background-color: #eebe77;*/
  width: 96%;
  height: 80px;
  border-radius: 20px;
  border: 1.5px solid var(--el-color-warning);
  /*box-shadow: 1px 1px 1px 1px rgb(0, 0, 0, 0.2);*/
}
.item {
  margin-left: 10%;
  margin-top: 10%;
  display: flex;
  align-items: flex-start;
}
.label {
  font-size: 12px;
  display: inline-block;
  margin-right: 20px;
}
.input-box {
  margin-left: 10px;
  width: 300px;
  height: 60px;
  border: none;
  outline: none;
  border-radius: 25px;
  padding-left: 20px;
  font-size: 16px;
  box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.2);
}
.mid-card {
  color: var(--el-color-warning);
  height: 30%;
  flex: 0.7; /* 设置 mid-card 的伸缩比例为 1，使其占据剩余空间 */
  text-align: center; /* 水平居中对齐内容 */
}
.right-card {
  float: right;
  display: inline-block;
  width: 30%;
  height: 80vh;
  margin-right: 10%;
  border-radius: 36px; /* 设置圆角的半径，可以根据需要调整 */
}
.work-head {
  /*border: 1px #000 solid;*/
  width: 465px;
  height: 68px;
  margin-top: 100px;
  margin-left: 300px;
}
.work-title {
  /*border: 1px #000 solid;*/
  color: #25282b;
  font-size: 26px;
  font-weight: 500;
}
.work-date {
  font-size: 26px;
  color: var(--el-color-warning);
  margin-top: 10px;
}
.add {
  font-size: 30px;
  color: var(--el-color-warning);
  float: right;
}
.workList {
  width: 465px;
  height: 600px;
  margin-left: 500px;
  display: inline-block;
  /*background: #e9ebef;*/
  box-shadow: 3px 3px 3px 3px rgb(197, 196, 196);
  border-radius: 8px 8px 8px 8px;
}
.workList-item {
  border: 1px var(--el-color-warning) solid;
  width: 465px;
  height: 150px;
  /*display: flex;*/
  border-radius: 8px 8px 8px 8px;
}
.workList-head {
  /*border: 1px #000 solid;*/
  display: flex;
  width: 465px;
  height: 55px;
}
.work_img {
  margin-top: 10px;
  margin-left: 10px;
}
.workList-action {
  /*border: 1px #000 solid;*/
  width: 160px;
  height: 28px;
  margin-left: 10px;
  margin-top: 19px;
  font-size: 20px;
  font-weight: 500;
}

.workList-time {
  /*border: 1px #000 solid;*/
  width: 100px;
  height: 30px;
  margin-left: 160px;
  margin-top: 20px;
}
.workList-action-content {
  /*border: 1px #000 solid;*/
  font-size: 14px;
  width: 300px;
  height: 68px;
  margin-top: 6px;
  margin-left: 60px;
}
.work-detail {
  border: 1px var(--el-color-warning) solid;
  width: 465px;
  height: 598px;
  margin-left: 800px;
  margin-top: -598px;
  box-shadow: 3px 3px 3px 3px rgb(197, 196, 196);
  border-radius: 8px 8px 8px 8px;
}

.work-title2 {
  /*border: 1px #000 solid;*/
  color: var(--el-color-warning);
  font-size: 30px;
  font-weight: 500;
  margin-top: 10px;
  margin-left: 20px;
  /*padding-top: 10px;*/
}

.work-detail-title {
  /*border: 1px #000 solid;*/
  color: #a6a6a6;
  font-size: 26px;
  font-weight: 500;
  margin-top: 10px;
  margin-left: 20px;
}
.work-detail-time {
  color: #a6a6a6;
  font-size: 18px;
  font-weight: 500;
  margin-top: 10px;
  margin-left: 20px;
}
.work-detail-content {
  color: #a6a6a6;
  font-size: 26px;
  font-weight: 500;
  margin-top: 50px;
  margin-left: 20px;
}
.work-detail-placeholder {
  width: 420px;
  height: 360px;
  margin-left: 20px;
  margin-top: 10px;
  font-size: 16px;
  font-weight: 500;
  border-radius: 8px;
  border: 2px solid var(--el-color-warning);
}
</style>
